<?php

require_once WP_PLUGIN_DIR . '/diapucp-events/defines.php' ;
require_once DPEM_SMB_PATH . '/smb_basefield.php' ;

define( 'TIMEPICK_CSS_FILE', '/css-timepicker/jquery-ui-timepicker.css' );
define( 'TIMEPICK_JSC_FILE', '/jquery-timepicker/jquery.ui.timepicker.min.js' );

define( 'TIMEPICK_CSS_DIRPATH', DPEM_CSS_DIRPATH . TIMEPICK_CSS_FILE );
define( 'TIMEPICK_CSS_URLPATH', DPEM_CSS_URLPATH . TIMEPICK_CSS_FILE );
define( 'TIMEPICK_JSC_DIRPATH', DPEM_JSC_DIRPATH . TIMEPICK_JSC_FILE );
define( 'TIMEPICK_JSC_URLPATH', DPEM_JSC_URLPATH . TIMEPICK_JSC_FILE );

class smartmeta_timepicker extends smb_basefieldbox {

    public function  __construct($fields) {
        $this->set_fields($fields);
        $this->set_inputid( $fields['id'] );
        
        //these are custom functions on the construct.
        add_action( 'admin_print_styles', array(&$this, 'add_stylesheets') );
        add_action( 'admin_enqueue_scripts', array(&$this, 'add_js') );
        add_action( 'admin_head-post.php', array(&$this, 'call_js') );
        add_action( 'admin_head-post-new.php', array( &$this, 'call_js' ) );
    }

    public static function build_me( $field_data, &$new_field_object ){
        $new_field_object = new smartmeta_timepicker($field_data);
    }

    public function show() {
        $value = $this->fetch_value();
        ?>
            <td>
                <label for="<?php echo $this->inputid; ?>"><?php echo $this->fields['name'] ?></label>
            </td>
            <td>
                <?php $this->add_required_label(); ?>
                <input
                    id="<?php echo $this->inputid; ?>"
                    name="<?php echo $this->inputid; ?>"
                    value="<?php echo esc_html($value) ?>"
                    type="text"
                    size="4"
                    readonly="yes"
                />
            </td>
        <?php
        $this->add_description();
    }

    public function save( $post_id ){
        $data = $_POST[ $this->inputid ];
        if ( empty( $data ) and isset($this->fields['default'] ) ){ $data = $this->fields['default']; }
        $data = wp_strip_all_tags($data);
        smb_basefieldbox::set($this->fields['id'], $data);
    }

    //Timepicker CSS calls
    public function add_stylesheets(){
        if( file_exists( TIMEPICK_CSS_DIRPATH ) ){
            wp_register_style( 'timepicker-css', TIMEPICK_CSS_URLPATH );
            wp_enqueue_style( 'timepicker-css' );
        }
   }

   //Timepicker JS calls
    public function add_js(){
        if( file_exists( TIMEPICK_JSC_DIRPATH ) ){
            wp_register_script( 'timepicker-js', TIMEPICK_JSC_URLPATH );
            wp_enqueue_script( 'timepicker-js' );
        }
    }

    public function call_js(){
        if ( !smb_basefieldbox::check_page( 'post' ) ) return;

        echo '<script type="text/javascript">';
            echo 'jQuery(document).ready(function(){';
            echo 'jQuery("#'. $this->inputid .'").timepicker();';
            echo '});';
        echo '</script>';
    }

} //End of the Class

?>
